package com.degon.hsyl.mychat;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SearchRecentSuggestionsProvider;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import io.rong.imkit.RongIM;
import io.rong.imkit.userinfo.RongUserInfoManager;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;

public class MainActivity2 extends AppCompatActivity {

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Button openConversationListActivity = findViewById(R.id.open_conversation_list);
        Button switchUser = findViewById(R.id.switch_user);
        TextView currentUser = findViewById(R.id.current_user);
        TextView tips = findViewById(R.id.tips);
        Button reloadData = findViewById(R.id.reload_data);
        reloadData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tips.setVisibility(View.GONE);
                String userName = getSharedPreferences("data", Context.MODE_PRIVATE).getString("userName", null);
                currentUser.setText("当前用户是:" + userName);
                currentUser.setVisibility(View.VISIBLE);
                switchUser.setVisibility(View.VISIBLE);
                openConversationListActivity.setVisibility(View.VISIBLE);
                reloadData.setVisibility(View.GONE);
                connectIM();
            }
        });

        openConversationListActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity2.this, ConversationListActivity.class);
                startActivity(intent);
            }
        });

        switchUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit();
                editor.putString("userToken", null);
                editor.putString("userName", null);
                editor.putString("headPortrait", null);
                editor.putString("rongYunId", null);
                editor.putString("rongYunToken", null);
                editor.apply();
                startActivity(new Intent(MainActivity2.this, MainActivity.class));
                finish();
            }
        });
    }

    private void connectIM(){
        String token = getSharedPreferences("data", Context.MODE_PRIVATE).getString("rongYunToken", null);
        RongIM.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onSuccess(String s) {
                ToastUtils.showShort("服务器已连接");
                setUserInfo();
            }

            @Override
            public void onError(RongIMClient.ConnectionErrorCode connectionErrorCode) {

            }

            @Override
            public void onDatabaseOpened(RongIMClient.DatabaseOpenStatus databaseOpenStatus) {

            }
        });
    }

    private void setUserInfo(){
        SharedPreferences preferences = getSharedPreferences("data", Context.MODE_PRIVATE);
        String userId = preferences.getString("rongYunId", null);
        String headPortrait = preferences.getString("headPortrait", null);
        String name = preferences.getString("userName", null);
        boolean isCacheUserInfo = true;
        RongUserInfoManager.getInstance().setUserInfoProvider(id -> new UserInfo(userId, name, Uri.parse(headPortrait)), isCacheUserInfo);
    }
}